﻿using Cdx.Bl.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cdx.Bl
{
    public class Engine
    {
        private string m_ConnectionString = null;

        private string m_WorkingFolder = null;
        
        public Engine(string connectionString, string workingFolder)
        {
            m_ConnectionString = connectionString;
            m_WorkingFolder = workingFolder;
        }

        public E_User Login(string userName, string password)
        {
            using (EngineModelContainer container = new EngineModelContainer(m_ConnectionString))
            {
                var user = container.E_UserSet.Where(u => u.Name == userName && u.Password == password).First();
                return user;
            }
        }

        public E_User CreateUser(string userName, string password)
        {
            using (EngineModelContainer container = new EngineModelContainer(m_ConnectionString))
            {
                var user = new E_User()
                {
                    Id = Guid.NewGuid(),
                    Name = userName,
                    Password = password
                };
                container.E_UserSet.Add(user);
                container.SaveChanges();

                var application = new E_Application()
                {
                    Id = Guid.NewGuid(),
                    Name = "Default",
                    Active = true
                };
                container.E_ApplicationSet.Add(application);

                container.R_UserApplicationSet.Add(new R_UserApplication()
                {
                    Id = Guid.NewGuid(),
                    E_User = user,
                    E_Application = application
                });

                container.SaveChanges();

                return user;
            }
        }

        public bool UserExists(string userName)
        {
            using (EngineModelContainer container = new EngineModelContainer(m_ConnectionString))
            {
                return container.E_UserSet.Any(u => u.Name == userName);
            }
        }

        public void CreateDocument(Guid userId, string documentName, byte[] data)
        {
            using (EngineModelContainer container = new EngineModelContainer(m_ConnectionString))
            {
                E_Application application = container.R_UserApplicationSet.Where(
                    r => r.E_User.Id == userId && r.E_Application.Active == true).First().E_Application;

                var filePath = Path.Combine(m_WorkingFolder, documentName);
                
                using(FileStream fileStream = File.Create(filePath))
                {
                    fileStream.Write(data, 0, data.Length);
                }

                application.E_Document.Add(new E_Document()
                {
                     Id = Guid.NewGuid(),
                     Name = documentName,
                     Path = filePath
                });

                container.SaveChanges();
            }                       
        }
    }
}
